<?php
if(PERMISSION != "F")
{
	redirectPage("index.php?module=logout");
	exit();
}

$clsEncypt = new clsEncrypt();
$clsEncypt->pubkey = "CrossWordGame";

if(isset($_POST["is_editing"]))
{
	if(file_exists($cw_data_url))
	{
		$raw_data = file_get_contents($cw_data_url);
		if($raw_data)
		{
			$data = unserialize($clsEncypt->decrypt(file_get_contents($cw_data_url)));
		}
		else
		{
			$data = array();
		}
		$title = setVar("title_vn","");
		$title = $title == "" ? "Untitled" : $title;
		$grid = setVar("grid_vn","");
		$legend = setVar("legend_vn","");
		$arr_legend = json_decode($legend, true);
		$word_count = count($arr_legend["across"]) + count($arr_legend["down"]);

		$data["vn"] = array("title"=>$title, "word_count"=>$word_count, "grid"=>$grid, "legend"=>$legend);

		$title = setVar("title_en","");
		$title = $title == "" ? "Untitled" : $title;
		$grid = setVar("grid_en","");
		$legend = setVar("legend_en","");
		$arr_legend = json_decode($legend, true);
		$word_count = count($arr_legend["across"]) + count($arr_legend["down"]);

		$data["en"] = array("title"=>$title, "word_count"=>$word_count, "grid"=>$grid, "legend"=>$legend);

		file_put_contents($cw_data_url, $clsEncypt->encrypt(serialize($data)));

		$msg = "Update successfuly!";
	}
	else
	{
		$msg = "Data not found!";
	}
	
	$msg = "Update successfuly!";
}

if(isset($_POST["is_active"]))
{
	if(file_exists($cw_data_url))
	{
		$raw_data = file_get_contents($cw_data_url);
		if($raw_data)
		{
			$data = unserialize($clsEncypt->decrypt(file_get_contents($cw_data_url)));
		}
		else
		{
			$data = array();
		}
		
		$active_cw = isset($_POST["active_cw"]) ? 1 : 0;
		$data["active_cw"] = $active_cw;
		file_put_contents($cw_data_url, $clsEncypt->encrypt(serialize($data)));

		$msg = "Update successfuly!";
	}
	else
	{
		$msg = "Data not found!";
	}
}


$data = array("active_cw"=>0,"en"=>array("title"=>"", "word_count"=>"", "grid"=>"", "legend"=>""),"vn"=>array("title"=>"", "word_count"=>"", "grid"=>"", "legend"=>""));

if(file_exists($cw_data_url))
{
	$raw_data = file_get_contents($cw_data_url);
	if($raw_data)
	{
		$data = unserialize($clsEncypt->decrypt(file_get_contents($cw_data_url)));
	}
	else
	{
		$msg = "Data error!";
	}
}
else
{
	$msg = "Data not found!";
}
?>
<?php include('includes/header.php'); ?>
<script type="text/javascript" src="<?=DIR_WS_USER?>js/home.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		$('#active_cw').click(function()
		{
			var checked = $(this).is(":checked");
			var message = checked == true ? "ACTIVATE" : "DEACTIVATE";
			if(confirm("Do you want to "+ message +" crossword game?"))
			{
				document.frmActive.submit();
			}
			else
			{
				$(this).prop('checked', !checked);
			}
		});
	});
</script>
<div id="content">
	<div>
		<form name="frmActive" action="" method="POST">
			<div class="active_checkbox" style="margin-top: 17px;margin-bottom: 10px;">
				<label for="active_cw">Crossword Game Active <input style="position: relative;top: 3px;" type="checkbox" value="1" name="active_cw" id="active_cw" <?php echo isset($data["active_cw"]) && $data["active_cw"] == 1 ? 'checked="checked"' : ''?>/></label>
			</div>
			<input type="hidden" name="is_active" value="0" />
		</form>
	</div>
	<div id="stage-1">
		<div class="clear"><?=isset($msg) ? $msg : ""?></div>
		<div class="col-1">
			<?php
			$arr_legend = json_decode($data["vn"]["legend"], true);
			if(count($arr_legend)>0)
			{
			$cross_word_data = legend_to_crossword_data($arr_legend);
			}
			?>
			<div class="vietnamese">
				<div style="font-size: 14px;float: left;width: 100%;margin-left: 12px;margin-bottom: 8px;font-weight: bold;width: 435px;">Vietnamese</div>
				<div id="build-crossword">
					<label for="crossword-title_vn">Tiêu đề</label>
					<input type="text" name="title_vn"  id="crossword-title_vn" value="<?php echo isset($data["vn"]["title"]) ? $data["vn"]["title"] : "" ?>" />
					<label for="crossword-data_vn">Nhập một từ, một khoảng trắng, và sau đó là đáp án hoặc gợi ý. Mỗi cặp từ / đáp án nằm trên mỗi dòng <a href="javascript:;" id="example_vn">Ví dụ cụ thể?</a></label>
					<textarea name="data_vn" wrap="off" id="crossword-data_vn">
					<?php echo isset($cross_word_data) ? $cross_word_data : "" ?>
					</textarea>
				</div>
			</div>
		</div>
		<div class="col-2">
			<?php
			$arr_legend = json_decode($data["en"]["legend"], true);
			if(count($arr_legend)>0)
			{
				$cross_word_data = legend_to_crossword_data($arr_legend);
			}
			?>
			<div class="english">
				<div style="font-size: 14px;float: left;margin-left: 12px;margin-bottom: 8px;font-weight: bold;width: 435px;">English</div>
				<div id="build-crossword">
					<label for="crossword-title_en">Crossword Title</label>
					<input type="text" name="title_en"  id="crossword-title_en" value="<?php echo isset($data["en"]["title"]) ? $data["en"]["title"] : "" ?>" />
					<label for="crossword-data_en">Enter the word, a space, and then the clue. One word/clue pair per line. <a href="javascript:;" id="example_en">Need to see an example?</a></label>
					<textarea name="data_en" wrap="off" id="crossword-data_en">
					<?php echo isset($cross_word_data) ? $cross_word_data : "" ?>
					</textarea>

				</div>
			</div>
		</div>
		<div class="clear"><br/><br/></div>
		<div class="button" style="cursor: pointer"><input type="button" name="submit" value="Generate" id="crossword-generate" /></div>
	</div>
	
	<div id="stage-2">       
	    <div class="button" id="finish-up">
	        <input type="button" value="Edit" id="edit" /> 
	        <input type="button" value="Regenerate" id="refresh" />  
	        
	        <form method="post" action="" style="display:inline;">
	            <input type="text" name="title_vn" id="title_json_vn" class="hide" />
	            <input type="text" name="title_en" id="title_json_en" class="hide" />
	            <textarea id="grid_json_vn" rows="50" cols="50" name="grid_vn" class='hide'></textarea>
	            <textarea id="grid_json_en" rows="50" cols="50" name="grid_en" class='hide'></textarea>
	            <textarea id="legend_json_vn" rows="50" cols="50" name="legend_vn" class="hide"></textarea>
	            <textarea id="legend_json_en" rows="50" cols="50" name="legend_en" class="hide"></textarea>
	            <input type="hidden" name="is_editing" value="0" />
	            <input type="submit" value="Update" />                    
	        </form>   
	    </div>
	    
	    <div id="fail">
	        <h2>Sorry! My algorithm failed to make you a crossword.</h2>
	        <p id="bad-words-message" class="bad-words">Try removing these words because they could not be placed on the crossword:</p>
	        <p id="bad-words" class="bad-words"></p>
	        <p><a href="javascript:void(0)" id="go-back">&lsaquo;&lsaquo; Back</a></p>
	    </div>         
		<div class="clear"></div>
		
		<div>
		<!--VN-->
		<div id="div_vn">
			<h2 id="title_vn"></h2>
			<div id="crossword_vn"></div>
			<div id="clues_vn">
				<div class="across-box" id="across-box_vn">
					<h5>Across</h5>
					<ol class="across"></ol>
				</div>
				<div class="down-box" id="down-box_vn">        
					<h5>Down</h5>
					<ol class="down"></ol>
				</div>
			</div>
		</div>
		<div class="clear"></div>
		<!--EN-->
		<div id="div_en">
			<h2 id="title_en"></h2>
			<div id="crossword_en"></div>
			<div id="clues_en">
				<div class="across-box" id="across-box_en">
					<h5>Across</h5>
					<ol class="across"></ol>
				</div>
				<div class="down-box" id="down-box_en">        
					<h5>Down</h5>
					<ol class="down"></ol>
				</div>
			</div>
		</div>
	    <div class="clear"></div>
		</div>
	</div>
	<div class="clear"></div>
	
	<div id="stage-3">
		<h3>Current crossword</h3>
		<div class="col-1">
			<div style="font-size: 14px;float: left;margin-left: 12px;margin-bottom: 8px;font-weight: bold;width: 435px;">Vietnamese</div>
			<table class='key crossword'>
					<?php
					$arr_grid = json_decode($data["vn"]["grid"], true);
					
					for($i = 0; $i < count($arr_grid); $i++)
					{
						$row = $arr_grid[$i];
					?>
					<tr>
					<?php
					for($j = 0; $j < count($row); $j++)
					{
						$cell = $row[$j];

						if(is_array($cell))//has char
						{
							$is_start_of_word = 0;
							$index = 0;
							$position = "";
							$char = $cell["char"];

							$across = $cell["across"];
							if(is_array($across))
							{
								$is_start_of_word = $across["is_start_of_word"] ? 1 : 0;
								if($is_start_of_word == 1)
								{
									$position = get_position_by_index($arr_legend, $across["index"], "across");
								}
							}

							$down = $cell["down"];
							if(is_array($down) && $is_start_of_word == 0)
							{
								$is_start_of_word = $down["is_start_of_word"];
								if($is_start_of_word == 1)
								{
									$position = get_position_by_index($arr_legend, $down["index"], "down");
								}
							}
							$style = $position ? "background-image:url('https://crosswordlabs.com/media/img/numbers/".($position).".png');" : "";
							?>
						<td style="<?=$style?>" title="<?=$i?>, <?=$j?>" class=""><?=$char?></td>
							<?
						}
						else//no char
						{
						?>
						<td title="<?=$i?>, <?=$j?>" class="no-border">
						&nbsp;
						</td>
						<?
						}
					}
					?>
					</tr>
					<?	
					}
					?>
				</table>
		</div>
		<div class="col-2">
			<div style="font-size: 14px;float: left;margin-left: 12px;margin-bottom: 8px;font-weight: bold;width: 435px;">English</div>
			<table class='key crossword'>
					<?php
					$arr_grid = json_decode($data["en"]["grid"], true);
					for($i = 0; $i < count($arr_grid); $i++)
					{
						$row = $arr_grid[$i];
					?>
					<tr>
					<?php
					for($j = 0; $j < count($row); $j++)
					{
						$cell = $row[$j];

						if(is_array($cell))//has char
						{
							$is_start_of_word = 0;
							$index = 0;
							$position = "";
							$char = $cell["char"];

							$across = $cell["across"];
							if(is_array($across))
							{
								$is_start_of_word = $across["is_start_of_word"] ? 1 : 0;
								if($is_start_of_word == 1)
								{
									$position = get_position_by_index($arr_legend, $across["index"], "across");
								}
							}

							$down = $cell["down"];
							if(is_array($down) && $is_start_of_word == 0)
							{
								$is_start_of_word = $down["is_start_of_word"];
								if($is_start_of_word == 1)
								{
									$position = get_position_by_index($arr_legend, $down["index"], "down");
								}
							}
							$style = $position ? "background-image:url('https://crosswordlabs.com/media/img/numbers/".($position).".png');" : "";
							?>
						<td style="<?=$style?>" title="<?=$i?>, <?=$j?>" class=""><?=$char?></td>
							<?
						}
						else//no char
						{
						?>
						<td title="<?=$i?>, <?=$j?>" class="no-border">
						&nbsp;
						</td>
						<?
						}
					}
					?>
					</tr>
					<?	
					}
					?>
				</table>
		</div>
	</div>
</div>

